package com.hns.can.safety.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hns.can.common.aop.orgTree.LeftOrgAnnotation;
import com.hns.can.common.aop.sedate.SedateAnnotation;
import com.hns.can.common.constants.GlobalConst;
import com.hns.can.common.formula.FillCodeDscValue;
import com.hns.can.common.formula.FillParamsVo;
import com.hns.can.safety.entity.AppMftPcsDetailEntity;
import com.hns.can.safety.vo.ReportFaultHandleParamsVo;
import com.hns.can.safety.vo.ReportFaultHandleVo;
import com.hns.frame.mybatis.Pagination;
import com.hns.frame.service.CommonService;
import com.hns.tool.pub.PubUtil;
import com.hns.tool.pub.UtilString;

/**
 * 报警处理service
 * @author:zhangy
 * @email:zhangyu2222z@sina.cn
 * @version Revision 2.0.0
 */
@Service
public class ReportFaultHandleService extends CommonService {

	@Autowired
    private FillCodeDscValue codeDscValue;
	
	private String[] fillCols = new String[]{"organName", "lineName", "licPltNo", "drvName"};
	private String[] relCols = new String[]{"organId", "lineId", "carId", "drvId"};
	private String[] mappingClazzs = new String[]{GlobalConst.CACHE_KEY_BASE_ORGAN,
			GlobalConst.CACHE_KEY_BASE_LINE_INFO,GlobalConst.CACHE_KEY_BASE_CAR_INFO,
			GlobalConst.CACHE_KEY_BASE_DRV_INFO};
	
	/**
	 * 查询报警处理
	 * @param params
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@SedateAnnotation()
	@LeftOrgAnnotation()
	@Transactional
	public Pagination searchReportFaultHandleDetail(ReportFaultHandleParamsVo params) throws Exception{
		Pagination page = this.myBatisPage("reportFaultHandle.searchReportFaultHandleDetailCnt", 
				"reportFaultHandle.searchReportFaultHandleDetailData", params, ReportFaultHandleVo.class);
		
		List list = page.getData();
//		HashMap map = new HashMap();
//		map.put("key3", new String[]{"carInCd"});
		fillNamedColVals(list, "car", null);
		page.setData(list);
		return page;
	}
	
	@SedateAnnotation()
	@LeftOrgAnnotation()
	@Transactional
	public List getExportFaultHandleData(ReportFaultHandleParamsVo params) throws Exception{
		List list = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("reportFaultHandle.searchReportFaultHandleDetailData", params), ReportFaultHandleVo.class);
//		HashMap map = new HashMap();
//		map.put("key3", new String[]{"carInCd"});
		fillNamedColVals(list, "car", null);
		return list;
	}
	
	@SedateAnnotation()
	public ReportFaultHandleVo getFaultHandleInfoById(ReportFaultHandleParamsVo params) {
//		AppMftPcsDetailEntity entity = this.commonEntityDao.find(params.getRcrdId(), AppMftPcsDetailEntity.class);
		ReportFaultHandleVo entity = null;
		List<ReportFaultHandleVo> list = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("reportFaultHandle.getReportFaultHandleDetailByMftId", params), ReportFaultHandleVo.class);
		if(PubUtil.isNotEmpty(list)){
			entity = list.get(0);
		}
		return entity;
	}
	
	
	@SuppressWarnings("unchecked")
	@Transactional
	public void uptFaultHandleInfo(AppMftPcsDetailEntity appMftPcsDetailEntity) {
//		AppMftPcsDetailEntity entity = (AppMftPcsDetailEntity)this.findById(appMftPcsDetailEntity.getRcrdId(), AppMftPcsDetailEntity.class);
		if(PubUtil.isNotEmpty(appMftPcsDetailEntity.getRcrdId())){
			AppMftPcsDetailEntity entity = this.commonEntityDao.find(appMftPcsDetailEntity.getRcrdId(), AppMftPcsDetailEntity.class);
//			 = (AppMftPcsDetailEntity)(list.get(0));
			entity.setPcsUser(this.getCurrentUser().getUser().getId());
			entity.setPcsTime(new Date());
			entity.setMftRsnAnl(appMftPcsDetailEntity.getMftRsnAnl());
			entity.setPcsOpin(appMftPcsDetailEntity.getPcsOpin());
			entity.setPcsWay(appMftPcsDetailEntity.getPcsWay());
			entity.setPcsSt(appMftPcsDetailEntity.getPcsSt());
			this.commonEntityDao.update(entity);
		}else{
			appMftPcsDetailEntity.setPcsUser(this.getCurrentUser().getUser().getId());
			this.sqlMapDao.insert("reportFaultHandle.addFaultHandleInfo", appMftPcsDetailEntity);
//			entity.setRcrdId(UtilString.getUUID());
//			entity.setMftId(appMftPcsDetailEntity.getMftId());
//			entity.setCrtTime(new Date());
//			entity.setPcsTime(new Date());
//			entity.setPcsUser(this.getCurrentUser().getUser().getId());
//			entity.setMftRsnAnl(appMftPcsDetailEntity.getMftRsnAnl());
//			entity.setPcsOpin(appMftPcsDetailEntity.getPcsOpin());
//			entity.setPcsWay(appMftPcsDetailEntity.getPcsWay());
//			entity.setPcsSt(appMftPcsDetailEntity.getPcsSt());
//			this.commonEntityDao.save(entity);
		}
	}
	
	
	/**
	 * 填充关联字段
	 * @param list
	 * @param type
	 * @throws Exception
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings("rawtypes")
	private void fillNamedColVals(List list, String type, Map nameMap) throws Exception{//organ, line, car, drv
		int i = 0;
		for(;i<relCols.length;i++){
			if(relCols[i].contains(type)){
				break;
			}
		}
		FillParamsVo[] vos = new FillParamsVo[(i+1)];
		String[] clazzs = new String[(i+1)];
		for(int j=0;j<=i;j++){
			Map<String,String> map = new HashMap<String,String>();
	        map.put(fillCols[j], GlobalConst.DEFAULT_CACHE_VALUE);
	        if(PubUtil.isNotEmpty(nameMap) && PubUtil.isNotEmpty(nameMap.get("key"+(j+1)))){
	        	String[] tmpFillNames = (String[])nameMap.get("key"+(j+1));
	        	for(String col : tmpFillNames){
	        		map.put(col, col);
	        	}
	        }
	        FillParamsVo vo = new FillParamsVo(relCols[j], map);
			vos[j] = vo;
			clazzs[j] = mappingClazzs[j];
		}
		codeDscValue.fillInfoValue(list, clazzs, vos);
	}

	@SedateAnnotation()
	public ReportFaultHandleVo getFaultHandleFormInfo(ReportFaultHandleParamsVo params) throws Exception{
		// TODO Auto-generated method stub
		ReportFaultHandleVo vo = null;
		List<ReportFaultHandleVo> list = PubUtil.convertListMapToObject(
				this.sqlMapDao.selectList("reportFaultHandle.getFaultHandleFormInfo", params), ReportFaultHandleVo.class);
		fillNamedColVals(list, "car", null);
		if(PubUtil.isNotEmpty(list)){
			vo = list.get(0);
		}
		return vo;
	}

//	@SedateAnnotation()
	public ReportFaultHandleVo getFaultHandleHistoryInfo(
			ReportFaultHandleParamsVo params) {
		// TODO Auto-generated method stub
		ReportFaultHandleVo vo = null;
		List<ReportFaultHandleVo> list = PubUtil.convertListMapToObject(
				this.sqlMapDao.selectList("reportFaultHandle.getFaultHandleHistoryInfo", params), ReportFaultHandleVo.class);
		if(PubUtil.isNotEmpty(list)){
			vo = list.get(0);
		}
		return vo;
	}


	


	

	
	
	
	
//	private void fillCodeDscValue(List list) throws Exception{
//        Map<String,String> map = new HashMap<String,String>();
//        map.put("lineName", GlobalConst.DEFAULT_CACHE_VALUE);
//        FillParamsVo vo1 = new FillParamsVo("lineId", map);
//        map = new HashMap<String,String>();
//        map.put("organName", GlobalConst.DEFAULT_CACHE_VALUE);
//        FillParamsVo vo2 = new FillParamsVo("organId", map);
//        map = new HashMap<String,String>();
//        map.put("licPltNo", GlobalConst.DEFAULT_CACHE_VALUE);
//        FillParamsVo vo4 = new FillParamsVo("carId", map);
//        FillParamsVo[] vos = new FillParamsVo[]{vo1, vo2, vo4};
//        codeDscValue.fillInfoValue(list,new String[]{GlobalConst.CACHE_KEY_BASE_LINE_INFO,GlobalConst.CACHE_KEY_BASE_ORGAN,GlobalConst.CACHE_KEY_BASE_CAR_INFO},vos);
//    }
	
}
